package com.example.rememberpwd;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class SaveUserInfo {
    //保存账号
    public static Boolean saveUserInfo(Context context,String account,String password) throws IOException {
        try {
            //通过上下文获取文件输出流
            FileOutputStream fos = context.openFileOutput("data.txt",context.MODE_PRIVATE);
            //把数据写到文件中
            fos.write((account+":"+password).getBytes());
            fos.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    //读取账号
    public static Map<String,String> getUserInfo(Context context) throws IOException {
        FileInputStream fis = null;
        String content = "";
        try {
            fis = context.openFileInput("data.txt");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            Map<String,String> userMap = new HashMap<String,String>();
            content = new String(buffer);
            String infos[] = content.split(":");
            userMap.put("account",infos[0]);
            userMap.put("password",infos[1]);
            fis.close();
            return userMap;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}

